// pages/topicDetails.js
const {
  gettribeByid,
  replyList,
  updcollectstate,
  updHelpnum,
	updoppsenum,
  fabu,
  iscollect
} = require('../../utils/api')
const { fetch } = require('../../utils/fetch')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    data: null,
    imgs: [],
    commonList: [],
    firstFlag: true,
    specificarea:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let {
      id
    } = options
    if(wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea:wx.getStorageSync('specificarea')
      })
    }

    fetch(`wx/tribe/${this.data.specificarea}/gettribeByid`, 'POST', {
      tribeid: id
    }).then(res => {
      this.setData({
        data: res,
        imgs: res.photohead ? res.photohead.split(',') : res.photohead
      })
      this.getReplyList(this.data.data.tribeid, this.data.data.area)
    })
    setTimeout(() => {
      this.getIscollect()
    }, 200)
  },
  onChange(event) {
    // event.detail 为当前输入的值
  },
  onConfirm(event) {
    fetch(`wx/reply/${this.data.specificarea}/fabu`, 'POST', {
      content: event.detail,
      memberid: wx.getStorageSync('userInfo').id,
      type: 4,
      typeid: this.data.data.tribeid,
      area: this.data.data.area,
    }).then(res => {
      this.setData({
        value: ''
      })
      this.getReplyList(this.data.data.tribeid, this.data.data.area)
    })
  },

  onClickIcon(e) {
    if (e.target.dataset.text == '收藏') {
      fetch(`wx/tribe/${this.data.specificarea}/updcollectstate`, 'POST', {
        tribeid: this.data.data.id,
        // sate: this.data.iscollect == 1 ? 0 : 1,
        memberId: wx.getStorageSync('userInfo').id
      }).then(res => {
        if (this.data.iscollection == '0') {
          wx.showToast({
            title: '收藏成功',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        } else {
          wx.showToast({
            title: '取消收藏成功',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
        this.getIscollect()
      })
    } else {
      fetch(`wx/tribe/${this.data.specificarea}/updHelpnum`, 'POST', {
        tribeid: this.data.data.id
      }).then(res => {
				wx.showToast({
					title: '已赞同',
					icon: 'none',
					duration: 1000,
					mask: true
				})
        this.setData({
					firstFlag:true,
          data: res,
          imgs: res.photohead ? res.photohead.split(',') : res.photohead
        })
        this.getReplyList(this.data.data.tribeid, this.data.data.area)
      })
    }
  },
	fanDui(){
		fetch(`wx/tribe/${this.data.specificarea}/updoppsenum`, 'POST', {
			tribeid: this.data.data.id
		}).then(res=>{
			wx.showToast({
				title: '已反对',
				icon: 'none',
				duration: 1000,
				mask: true
			})
			this.setData({
				firstFlag: false,
				data: res,
				imgs: res.photohead ? res.photohead.split(',') : res.photohead
			})
			this.getReplyList(this.data.data.tribeid, this.data.data.area)
		})
	},
  
  // 判断是否收藏当前话题
  getIscollect() {
    fetch(`wx/tribe/${this.data.specificarea}/iscollect`, 'POST', {
      tribeId: this.data.data.id,
      // area: wx.getStorageSync('userInfo').area,
      memberId: wx.getStorageSync('userInfo').id
    }).then(res => {

      this.setData({
        iscollection: res
      })
    })
  },
  // 获取评论
  getReplyList(tribeid, area) {
    fetch(`wx/reply/${this.data.specificarea}/list`, 'POST', {
      type: 4,
      typeid: tribeid,
      area: area
    }).then(res => {
      this.setData({
        commonList: res.list
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
	},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})